package com.wang.wx.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.core.io.ClassPathResource;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.nio.file.Files;

/**
 * @author wjh
 * @create 2024-08-05 14:09
 */
@Api(tags = "2、模板下载")
@RestController
@RequestMapping("/admin/system/download")
public class FileDownloadController {

    @ApiOperation(value = "邮箱转推特导入模版下载",produces = MediaType.APPLICATION_OCTET_STREAM_VALUE)
    @GetMapping("/emailToTwitter")
    public void emailToTwitter(HttpServletResponse response) throws IOException {
        String TEMPLATE_FILE_NAME = "邮箱转推特导入模版.xlsx";
        downloadFile(response, TEMPLATE_FILE_NAME);
    }


    @ApiOperation(value = "手机转邮箱导入模版下载",produces = MediaType.APPLICATION_OCTET_STREAM_VALUE)
    @GetMapping("/phoneToEmail")
    public void phoneToEmail(HttpServletResponse response) throws IOException {
        String TEMPLATE_FILE_NAME = "手机转邮箱导入模版.xlsx";
        downloadFile(response, TEMPLATE_FILE_NAME);
    }

    @ApiOperation(value = "手机转领英导入模版下载",produces = MediaType.APPLICATION_OCTET_STREAM_VALUE)
    @GetMapping("/phoneToLy")
    public void phoneToLy(HttpServletResponse response) throws IOException {
        String TEMPLATE_FILE_NAME = "手机转领英导入模版.xlsx";
        downloadFile(response, TEMPLATE_FILE_NAME);
    }


    @ApiOperation(value = "手机转微信ID导入模版下载",produces = MediaType.APPLICATION_OCTET_STREAM_VALUE)
    @GetMapping("/phoneToWx")
    public void phoneToWx(HttpServletResponse response) throws IOException {
        String TEMPLATE_FILE_NAME = "手机转微信ID导入模版.xlsx";
        downloadFile(response, TEMPLATE_FILE_NAME);
    }


    @ApiOperation(value = "邮箱转领英导入模版下载",produces = MediaType.APPLICATION_OCTET_STREAM_VALUE)
    @GetMapping("/emailToLy")
    public void emailToLy(HttpServletResponse response) throws IOException {
        String TEMPLATE_FILE_NAME = "邮箱转领英导入模版.xlsx";
        downloadFile(response, TEMPLATE_FILE_NAME);
    }

    private void downloadFile(HttpServletResponse response, String TEMPLATE_FILE_NAME) throws IOException {
        try {
            // 获取模板文件的路径
            ClassPathResource resource = new ClassPathResource(TEMPLATE_FILE_NAME);

            // 设置输出流的格式
            response.addHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(TEMPLATE_FILE_NAME, "UTF-8"));
            response.setContentType("application/octet-stream");
            response.setHeader("Cache-Control", "no-cache, no-store, must-revalidate");
            response.setHeader("Pragma", "no-cache");
            response.setDateHeader("Expires", 0);

            // 读取文件的字节流
            byte[] bytes = Files.readAllBytes(resource.getFile().toPath());
            response.getOutputStream().write(bytes);
            response.getOutputStream().flush();
            response.getOutputStream().close();
        } catch (IOException e) {
            // 处理文件不存在或无法读取的情况
            e.printStackTrace();
            response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            response.getWriter().write("Error downloading the file: " + e.getMessage());
        }
    }
}
